<?php
	$path_one = 'admin/admin_project';
	$path_two = 'admin/admin_project_address';
	$filename = 'admin/device_workplace_list';
	require_once("header.php");
	require_once("admin_comm.php");
	require_once $_site->get_handlepath('project');
	require_once $_site->get_handlepath('device');
	$page=request('page',false,true,false,1);
	$pagesize=request('pagesize',false,true,false,100);
	$data['pid']=request('pid',false,true,false,0);
	$data['state']=request('state',false,true,false,0);
	$data['address']=request('address',true,false,false,0);//作业点id
	$data['no']=request('no',true,false,false,0);
	$no_title=request('no_title');

	$start_time=$data['start_time']=request('start_time');
	$end_time=$data['end_time']=request('end_time');

	$data['start_time'] = $data['start_time']?strtotime($data['start_time']):0;
	$data['end_time'] = $data['end_time']?strtotime($data['end_time'])+3481:0;


	$result = device_fun_get_checkin_list($data,$page,$pagesize);
	
	$hover[$data['state']]=' class="hover" ';
	$address_list = $data['pid']&&$data['address']==false?$project->get_project_address_list(array('all'=>true,'pid'=>$data['pid'])):false;//作业点列表
	if($data['address']){
		$address_info = project_fun_get_project_address_info($data['address']);
	}
	$carType=array(1=>"挖掘机",2=>"运输车");
?>
<script type="text/javascript" charset="utf-8">

	loader.file({
		plugin:['validform/main','dialog/main','my97/wdatepicker'],
		skincss:['list']
	})
	function get_project_info (){
		dialog.iframe({
			title:'项目选择',
			is_resize:false,
			top:{
				is_show:true,
				btn_max:false,
				btn_min:false,
				btn_close:true,
				height:33,
				is_move:true
			},
			is_scroll:false,
			min_width:350,
			min_height:500,
			is_center:true,
			is_lock_back:false,
			content:'/admin/common_choise?callback=set_project_info&parm=state.1',
			name:'choise_project'
		});
	}

	function set_project_info(id,title){
		if(id && title){
			$("#pid").val(id);
			$("#name_title").val(title);
			get_address_list(id);
		}
	}

	function get_address_list(pid,coord){
		if(!pid) return false;
		$.ajax({
			type:'get',//Get or Post
			url:'/handle.php',
			cache:false,//false
			dataType:'json',
			data:'h=project/address_list&pid='+pid,
			beforeSend:function(XMLHttpRequest){

			},
			success:function(data){

				if(data.list){
					var strHtml = '<option value="">≡选择最近的作业点≡</option>';
					for (var i in data.list){
						//var coord = data.list[i]['coord_x']+','+data.list[i]['coord_y'];
						var h = data.list[i]['id']=='<?php echo $data['address'];?>'?'selected':'';
						//var h = coord=='<?php //echo $data['address'];?>'?'selected':'';
						//strHtml+='<option '+h+' value="'+coord+'">'+data.list[i]['title']+'</option>';
						strHtml+='<option '+h+' value="'+data.list[i]['id']+'">'+data.list[i]['title']+'</option>';
					}

				}else{
					var strHtml = '<option value="">≡选择最近的作业点≡</option>';
				}
				$('#address').html(strHtml);
			}
		});//Ajax End
	}
	window.onload=function (){
		get_address_list('<?php echo $data['pid'];?>','<?php echo $data['address'];?>');


	}

	function import_out(obj){
		var _this=$(obj);
		var item_id = '<?php echo $data['pid'];?>';
		if(item_id==null || item_id==false){
			alert('请先选择项目');
			return ;
		}
		var request_url = _this.parents('form').serialize();
		window.location.href="/handle.php?h=device/improt_out&"+request_url;
		return false;

	}
	function zippack(obj){
		var _this=$(obj);
		var item_id = '<?php echo $data['pid'];?>';
		var item_name = '<?php echo $no_title;?>';
		var start_time='<?php echo $data['start_time'];?>'
		var end_time='<?php echo $data['end_time'];?>'
		if(!item_id || !item_name){
			alert('请先选择项目');
			return ;
		}
		window.location.href='/handle.php?h=device/zip_pack&item_id='+item_id+'&item_name='+item_name+'&start_time='+start_time+'&end_time='+end_time;
	}

	function deltime(){
		var item_id = '<?php echo $data['pid'];?>';
		if(item_id==null || item_id==false){
			alert('请先选择项目');
			return ;
		}
		var times = $('#time_limit').val();
		if(times == ''){
			alert("请选择删除时间");
			return ;
		}
		if(!confirm("确定要将该项目"+times+"之前的登记数据删除吗？\n请注意：删除后将不能恢复")){
			return ;
		}
		$.ajax({
			type:'get',//Get or Post
			url:'/handle.php',
			cache:false,//false
			dataType:'json',
			data:'h=device/checkin_del&pid=<?php echo $data['pid'];?>&times='+times,
			beforeSend:function(XMLHttpRequest){

			},
			success:function(data){
				if(data.state){
					alert('成功删除'+data.row+"条数据");
					window.location.reload();
					return ;
				}
				alert('删除 '+data.row+" 条数据 \n你选择的数据已经删除过了 或者没有该数据");
				window.location.reload();
				/*dialog.finish({
					content:data.msg,
					success:data.state,
					callback:function (){
						if(data.state){

						}
					}
				})*/
			}
		});//Ajax End
	}
</script>
</head>
<body>
	<div class="position_nav">你所在的当前位置：<?php echo $path_str;?></div>
		<div class="main">

			<div class="sos_box">
				<form action="/admin/admin_address_device_log" id="search" method='get'>
					<span>选择项目：</span>
						<input type="hidden" name="pid" id='pid' value="<?php echo $data['pid'] ;?>">
						<input type="text" name='no_title' id='name_title' readonly="readonly" value="<?php echo $no_title ;?>" onclick='get_project_info();'>
					<span>作业点：</span>
					<select name='address' id='address'>

					</select>
					<span>手机设备号：</span>
					<input type="text" name="no" value="<?php echo $data['no'] ;?>">
					<div style="clear: both;width:100%;*height:0px;margin:5px 0;"></div>
					<span>按登记时间段搜索：</span>
					<style type="text/css">
						#d4311,#d4312,#time_limit{background: url('http://www.my97.net/dp/My97DatePicker/skin/datePicker.gif') no-repeat right center;}
					</style>
					<input class='date_icon' type="text" id="d4311" name="start_time" value="<?php echo $start_time ;?>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:00:00',maxDate:'#F{$dp.$D(\'d4312\')||\'<?php echo date('Y-m-d');?>\'}',readOnly:true})"> -
					<input type="text" id="d4312" name="end_time" value="<?php echo $end_time ;?>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:00:00',minDate:'#F{$dp.$D(\'d4311\')}',maxDate:'<?php echo date('Y-m-d');?>',readOnly:true})">

					<input class="button1" type="submit" value="搜索">

					<input class="button1" type="button" onclick="import_out(this);return false;" value="数据导出">
					<input class="button1" type="button" onclick="zippack(this);return false;" value="项目图片下载">
					<div style="clear: both;width:100%;*height:25px;margin:5px 0;">
						<b class="red">提示:</b>输入了条件后。需要搜索过后，后面的导出，下载才会根据条件生效。（项目的图片下载只对日期条件有效）</div>
					<div style="clear: both;width:100%;*height:0px;margin:5px 0;"></div>
				</form>
				删除 <input type="text" name="time_limit" id="time_limit" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'<?php echo date('Y-m-d',strtotime('-7 day'));?>',readOnly:true})" value=""> 之前的登记记录
				<input class="button1" type="button" onclick="deltime()" value="确定删除">
			</div>
			<table cellpadding="0" cellspacing="1" border="0" class="table">
				<tr>
					<th align="left" width="6%">流水号</th>
					<!--<th align="left">所属项目</th>-->
					<th align="left" width="8%">车牌号</th>
					<th align="left" width="12%">手机设备号</th>
					<th align="left" width="28%">距离</th>
					<!--<th>坐标</th>-->
					<th align="left" width="110px">登记照片</th>
					<th align="left" width="8%">照片路径</th>
					<th align="left" width="8%">登记时间</th>
					<th align="left" width="8%">服务器时间</th>
					<th align="left">类型</th>
				</tr>
				<?php
					if($result['rows']){
						foreach ($result['rows'] as $v){
							$distance=array();
							if($address_list){
								foreach ($address_list as $k=>$list){
									list($addr_coord_x,$addr_coord_y)=array($list['coord_x'],$list['coord_y']);
									$distance[$k]=$list;
									list($coord_x,$coord_y)=array($v['coord_x'],$v['coord_y']);
									$distance[$k]['dis'] = 12756274 * asin(
									sqrt(
											pow(
												sin((deg2rad($addr_coord_y)-deg2rad($coord_y))/2),2
											)
											+
											cos(deg2rad($addr_coord_y))*cos(deg2rad($coord_y))
											*
											pow(sin((deg2rad($addr_coord_x)-deg2rad($coord_x))/2),2)
										)
									);


								}
							}
							if($distance){

								$OK=array();
								foreach ($distance as $key=>$vlist){
									if($vlist['dis']<$vlist['distance']){
										//设备与作业点合法的距离
										$OK[$key] = $vlist;
										$OK[$key]['state'] = 1;//状态合法
									}
								}
								if(!$OK){
									$OK[0] = $distance[0];//最近的作业点
									foreach ($distance as $key=>$vlist){
										if($OK[0]['dis']>=$distance[$key+1]['dis'] && $distance[$key+1]){
											$OK[0]=$distance[$key+1];
											$OK[0]['state']=$OK[0]['dis']>$OK[0]['distance']?0:1;

										}
									}
								}
							}else{
								$OK[0] = $address_info;
								$OK[0]['dis'] = $v['distances'];
								$OK[0]['state'] = $OK[0]['dis']>$OK[0]['distance']?0:1;
							}
							if($OK){
								$address_list_val='';
								foreach ($OK as $v2){
									$address_list_val.="<p style='line-height:20px;'>作业点：{$v2['title']}<br />作业点范围：{$v2['distance']}M<br />作业点与设备距离：".(round($v2['dis'],3))."M<br />状态：".($v2['state']?"合法":($v['coord_x']>1&&$v['coord_y']>1 ? "不合法":"数据异常(坐标未获取)"))."</p><hr>";
								}
							}
							//printf('%s=>%.1f',"最小值",$minVal/1000)

							?>
							<tr>
								<td valign="top"><?php echo $v['id'] ;?></td>
								<td valign="top"><?php echo $v['car_number'] ;?></td>
								<td valign="top"><?php echo $v['no'] ;?></td>
								<td ><?php echo $address_list_val ;?></td>
								<!--<td><?php echo $v['coord_x'].','.$v['coord_y'] ;?></td>-->
								<td valign="top"><img src='<?php $result; echo $v['img'] ;?>' width="100" height='80'></td>
								<td valign="top"><?php echo str_replace('/upload/item','',$v['img']) ;?></td>
								<?php
								//原来的方法，搞不懂为什么除1000
								  if(13==strlen($v['ctime'])){ 
								?>
								<td valign="top"><?php echo date('Y-m-d H:i',(int)$v['ctime']/1000); ?></td>
								<?php 
								}else{
								?> 
								<td valign="top"><?php echo date('Y-m-d H:i',$v['ctime']); ?>
								<?php 
								}
								?>
								<td valign="top"><?php echo date('Y-m-d H:i',$v['time']) ;?></td>
								<td valign="top"><?php echo $carType[$v['type']];?></td>
							</tr>
							<?php
						}
					}else{
						?>
						<tr>
							<td colspan="10">暂无数据！</td>
						</tr>
						<?php
					}
				?>
				<tr>
					<td colspan="10">
						<div class="list_more">
							项目总数：<?php echo $result['row_count']?$result['row_count']:0 ;?>
							<?php
								echo page_method($result['row_count'], $page, $result['page_count'], 10, '?'.preg_replace('/(&page=\d*)/','',$_SERVER['QUERY_STRING']), false);
							?>
						</div>
					</td>
				</tr>
			</table>
		</div>
	</div>
</body>
</html>


